import { createSlice } from "@reduxjs/toolkit"
export const goodSlice = createSlice({
  name: "goods",
  initialState: {
    goodsList: [
      {
        num: "5",
        price: "12.00",
        desc: "小米手机pros",
        title: "小米手机",
        id: 0,
        isDone: false
      },
      {
        num: "10",
        price: "120.00",
        desc: "苹果手机pros",
        title: "苹果手机",
        id: 1,
        isDone: false
      },
      {
        num: "55",
        price: "1200.00",
        desc: "华为手机pros",
        title: "华为手机",
        id: 2,
        isDone: true
      }
    ],
    newList: [{
      num: "5",
      price: "12.00",
      desc: "小米手机pros",
      title: "小米手机",
      id: 0,
      isDone: false
    },
    {
      num: "10",
      price: "120.00",
      desc: "苹果手机pros",
      title: "苹果手机",
      id: 1,
      isDone: false
    },
    {
      num: "55",
      price: "1200.00",
      desc: "华为手机pros",
      title: "华为手机",
      id: 2,
      isDone: true
    }]
  },
  reducers: {
    add: (state, { payload }) => {
      state.goodsList.unshift({ desc: payload, isDone: false, id: new Date().getTime() })
      state.newList = state.goodsList
    },
    changeOne: (state, { payload }) => {
      state.goodsList = state.goodsList.map(item => {
        if (item.id === payload) {
          item.isDone = !item.isDone
        }
        return item
      })
      state.newList = state.goodsList
    },
    changeAll: (state, { payload }) => {
      state.goodsList.map(item => item.isDone = payload)
      state.newList = state.goodsList
    },
    setList: (state, { payload }) => {
      if (payload === 1) {
        state.newList = state.goodsList
      } else if (payload === 2) {
        state.newList = state.goodsList.filter(item => item.isDone)
      } else if (payload === 3) {
        state.newList = state.goodsList.filter(item => !item.isDone)
      }
    },
    del:(state,{payload})=>{
      state.goodsList = state.goodsList.filter(item=> item.id!==payload)
      state.newList = state.goodsList
    }
  }
})
export const { add, changeOne, changeAll, setList,del } = goodSlice.actions
export default goodSlice.reducer